package org.ichow.question.plugins
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * ...
	 * SingleChoice 单选题
	 * @author M.ichow
	 */
	public class SingleChoicePlugin extends ChoicePlugin
	{
		public static const VERSION:Number = 1.0;
		public static const API:Number = 1.0;
		//show selected textfield
		protected var choiceTf : TextField;
		//choice sign eg: ["ABCD"]
		protected var choiceSigns : Array;
		//single or multi
		protected var isSingle : Boolean = true;
		//selected string
		protected var choiceTxt : String;
		
		public function SingleChoicePlugin()
		{
			this.propName = "SingleChoicePlugin";
		}
		
		// ---------------------------------- Protected Methods ---------------------------------- //
		
		/**
		 * get assets
		 * initialize plugin
		 */
		override protected function init() : void
		{
			super.init();
			choiceSigns = [];
			choiceTxt = "";
			choiceTf = getChild( "txt" ) as TextField;
			var sign : String = getData( "choiceSign" ) as String;
			
			if ( sign )
			{
				choiceSigns = sign.indexOf( "|" ) == -1 ? sign.split( "" ) : sign.split( "|" );
			}
		}
		
		/**
		 * update choiced array && choiced tf
		 * @param	mc
		 */
		override protected function update( mc : MovieClip = null ) : void
		{
			super.update( mc );
			if ( !mc )
				return;
			var index : int = isSingle ? 0 : getIndex( mc.name );
			
			if ( !choicedAry[ index ] )
				choicedAry[ index ] = mc;
			else if ( choicedAry[ index ] != mc )
			{
				choicedAry[ index ].gotoAndStop( 1 );
				choicedAry[ index ] = mc;
			}
			else
				choicedAry[ index ] = null;
			
			choiceTxt = "";
			for ( var i : int = 0 ; i < choicedAry.length ; i++ )
			{
				if ( choicedAry[ i ] != null )
				{
					choiceTxt += choiceSigns[ getIndex( choicedAry[ i ].name ) ];
				}
			}
			if ( choiceTf )
				choiceTf.text = choiceTxt;
		}
		
		// ---------------------------------- Public Methods ---------------------------------- //
		
		/**
		 * check plugin
		 * @return Boolean
		 */
		override public function check() : Boolean
		{
			if ( choiceTxt == answer[ 0 ] )
				return true;
			return false;
		}
		
		/**
		 * reset plugin
		 */
		override public function reset() : void
		{
			super.reset();
			choiceTxt = "";
			choicedAry = [];
			if ( choiceTf )
				choiceTf.text = "";
		}
		
		/**
		 * get score
		 */
		override public function get score() : Number
		{
			return check() ? tScore : 0;
		}
	
	}

}